
import os
import sys
import pandas as pd
from src.missing.ml_fill import ml_fill_missing_values

# 设置项目根目录
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../"))
if BASE_DIR not in sys.path:
    sys.path.insert(0, BASE_DIR)

# 输入输出路径配置
input_path = os.path.join(BASE_DIR, "data", "2_missing_value_analysis", "filled", "needFill_indicators_filled.csv")
output_path = os.path.join(BASE_DIR, "data", "2_missing_value_analysis", "filled", "needFill_indicators_filled_ML.csv")

# 读取原始数据
df_before = pd.read_csv(input_path, encoding="utf-8-sig")

# 计算并输出填补前缺失比例报告
missing_ratios_before = df_before.isna().mean() * 100
missing_report_before = missing_ratios_before[missing_ratios_before > 0].sort_values(ascending=False)
print("📊 填补前各字段缺失比例：")
print(missing_report_before.round(2).to_string())

# 执行机器学习填补
ml_fill_missing_values(input_path, output_path)

# 读取填补后的数据
df_after = pd.read_csv(output_path, encoding="utf-8-sig")

# 输出填补后缺失比例报告
missing_ratios_after = df_after.isna().mean() * 100
missing_report_after = missing_ratios_after[missing_ratios_after > 0].sort_values(ascending=False)

print("\n📊 填补后仍存在缺失的字段：")
if not missing_report_after.empty:
    print(missing_report_after.round(2).to_string())
else:
    print("✅ 所有字段已完成填补，无缺失值。")
